using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Security.Policy;
using UnityEngine;
using UnityEngine.Networking;
/// <summary>
/// 资源帮助类
/// </summary>
public static class HFAssetBundleHelper
{
	#region LoadFileByDecryptFunc 资源包加解密
	/// <summary>
	/// 解密加载的文件
	/// </summary>
	public static Func<byte[], byte[]> LoadFileByDecryptFunc;

	/// <summary>
	/// 解码数据流
	/// </summary>
	/// <param name="_bytes">文件二进制数据</param>
	/// <returns>文件数据</returns>
	static byte[] OnDecryptBinary(byte[] _bytes)
	{
		if (LoadFileByDecryptFunc != null)
		{
			_bytes = LoadFileByDecryptFunc(_bytes);
		}
		return _bytes;
	}
    #endregion

    #region GetAssetBundleFullPath 获得资源FullPath
    /// <summary>
    /// 获得资源FullPath
    /// </summary>
    /// <param name="_path">资源路径</param>
    /// <param name="_loadModel">加载模式</param>
    /// <param name="_isUseStreamingAssets">是否使用 </param>
    /// <returns>资源路径</returns>
    public static string GetAssetBundleFullPath(string _path, enHFAbLoadModel _loadModel, out bool _isUseStreamingAssets)
	{		
		string path = string.Empty;
		_isUseStreamingAssets = false;
		switch (_loadModel)
		{
			case enHFAbLoadModel.Editor:
				path = _path;
				break;
			case enHFAbLoadModel.PersistentData:
				path = Path.Combine(SFGlobalSetting.assetBundleRoot, _path);
				if (!File.Exists(path))
				{
					path = Path.Combine(SFGlobalSetting.streamingAssetsRoot, _path);
					_isUseStreamingAssets = true;
				}
				break;
			case enHFAbLoadModel.StreamingAssets:
				path = Path.Combine(SFGlobalSetting.streamingAssetsRoot, _path);
				_isUseStreamingAssets = true;
				break;
			case enHFAbLoadModel.CDNAssets:
                path = Path.Combine(SFGlobalSetting.cdnassetBundleRoot, _path);
                break;
        }
		return path;
	}
	#endregion

	#region GetAssetBundleManifestFullPath 获得AssetBundleManifest文件FullPath
	/// <summary>
	/// 获得AssetBundleManifest文件FullPath
	/// </summary>
	/// <param name="_loadModel">加载模式</param>
	/// <param name="_isUseStreamingAssets">是否使用 </param>
	/// <returns>资源路径</returns>
	public static string GetAssetBundleManifestFullPath(enHFAbLoadModel _loadModel, out bool _isUseStreamingAssets)
	{
		string path = string.Empty;
		_isUseStreamingAssets = false;
		switch (_loadModel)
		{
			case enHFAbLoadModel.Editor:
				break;
			case enHFAbLoadModel.PersistentData:
				path = SFGlobalSetting.manifestAssetBundleRootPath;
				if (!File.Exists(path))
				{
					path = SFGlobalSetting.manifestStreamingAssetsRootPath;
					_isUseStreamingAssets = true;
				}
				break;
			case enHFAbLoadModel.StreamingAssets:
				path = SFGlobalSetting.manifestStreamingAssetsRootPath;
				_isUseStreamingAssets = true;
				break;
            case enHFAbLoadModel.CDNAssets:
                path = Path.Combine(SFGlobalSetting.cdnmanifestAssetBundleRootPath);
                break;

        }
		return path;
	}
	#endregion

	#region ReadBuffer 读取文件流r
	/// <summary>
	/// 读取文件流
	/// </summary>
	/// <param name="_path">文件路径</param>
	/// <param name="_isUseStreamingAssets">是否是StreamingAssets资源</param>
	/// <returns>文件流</returns>
	public static byte[] ReadBuffer(string _path, bool _isUseStreamingAssets)
	{
		byte[] buffer = null;
		if (_isUseStreamingAssets)
		{
			buffer = OnReadStreamingAssets(_path);
		}
		else
		{
			buffer = File.ReadAllBytes(_path);
		}
		return OnDecryptBinary(buffer);
	}
	/// <summary>
	/// 读取Byte[] 
	/// </summary>
	/// <param name="_path"></param>
	/// <param name="_isUseStreamingAssets"></param>
	/// <param name="outbyties"></param>
	/// <returns></returns>
	public static bool TryReadBuffer(string _path, enHFAbLoadModel loadModel, bool _isUseStreamingAssets,out byte[] outbyties) {
		bool hadPath = false;
		outbyties = null;
		if (_isUseStreamingAssets)
        {
			outbyties = OnReadStreamingAssets(_path);
			hadPath = outbyties != null && outbyties.Length > 0;
		}
		else
		{
			if (loadModel != enHFAbLoadModel.CDNAssets)
			{
                if (File.Exists(_path))
                {
                    outbyties = File.ReadAllBytes(_path);
                    hadPath = true;
                }
            }
           
		}
		OnDecryptBinary(outbyties);
		return hadPath;

	}

	/// <summary>
	/// 获得StreamingAssets文件数据流
	/// </summary>
	/// <param name="_streamingAssetsPath">StreamingAssets路径</param>
	/// <returns>资源包</returns>
	static byte[] OnReadStreamingAssets(string _streamingAssetsPath)
	{
		UnityWebRequest getRequest = UnityWebRequest.Get(_streamingAssetsPath);
		var async = getRequest.SendWebRequest();
		while (!async.isDone)
		{

		}
		byte[] result = getRequest.downloadHandler.data;		
		return result;
	}
	#endregion
}

